package View;

import java.awt.Dimension;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import Model.TrafficLight;
import java.util.Timer;
import java.util.TimerTask;

/**
 *
 * @author Simon Marshall
 * @author jc209673
 */
public class Display extends JPanel {

    private TrafficLight horizontalLight;
    private TrafficLight verticalLight;
    private Timer timer;

    /**
     * Sets the background colour to Green (0x008000) and sets the GUI size to a static 600 pixels in width by  approximately 600 pixels  in height
     */
    public Display() {
        setBackground(new Color(0x008000));
        setPreferredSize(new Dimension(600, 600));
        timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                repaint();
            }
        }, 1000, 1000);
    }

    /**
     * 
     * @param h creates the local variable for horizontalLight
     * @param v creates the local variable for verticalLight
     */
    public void setup(TrafficLight h, TrafficLight v) {
        horizontalLight = h;
        verticalLight = v;
    }

    /**
     * repaints the GUI
     */
    public void run() {
        repaint();
    }

    /**
     *
     * @param g Draws Horizontal and Vertical Lanes and Traffic Lights for Horizontal and Vertical Lanes, as well as drawing the vehicles that will go in horizontal and vertical lanes.
     */
    @Override
    public void paintComponent(Graphics g) {
        g.setColor(new Color(0x008000));
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        g.setColor(Color.DARK_GRAY);

        g.fillRect(getWidth() / 2 - 30, 0, 60, getHeight()); // Sets the vertical lane
        g.fillRect(0, getHeight() / 2 - 30, getWidth(), 60); // Sets the horizontal lane

        g.setColor(Color.BLACK); // Draws and sets the colour for the Horizontal and Vertical Traffic Light Box
        g.fillRect(getWidth() / 2 + 50, 0, 90, 45); // Draws the Vertical Traffic Light
        g.fillRect(0, getHeight() / 3 - 10, 90, 45); // Draws the Horizontal Traffic Light

        if (horizontalLight == null) {
            return;
        }

        if (horizontalLight.getColor() == TrafficLight.RED) {
            g.setColor(Color.RED); // Draws red light for Horizontal Light
            g.fillOval(10, getHeight() / 3 + 5, 15, 15);

        } else if (horizontalLight.getColor() == TrafficLight.YELLOW) {
            g.setColor(Color.YELLOW); // Draws orange light for Horizontal Light
            g.fillOval(10 + 25, getHeight() / 3 + 5, 15, 15);

        } else if (horizontalLight.getColor() == TrafficLight.GREEN) {
            g.setColor(Color.GREEN); // Draws green light for Horizontal Light
            g.fillOval(10 + 50, getHeight() / 3 + 5, 15, 15);
        }

        if (verticalLight == null) {
            return;
        }

        if (verticalLight.getColor() == TrafficLight.RED) {
            g.setColor(Color.RED);
            g.fillOval(getWidth() / 2 + 65, 15, 15, 15); // Draws red light for Vertical Light

        } else if (verticalLight.getColor() == TrafficLight.YELLOW) {
            g.setColor(Color.YELLOW);
            g.fillOval(getWidth() / 2 + 90, 15, 15, 15); // Draws orange light for Vertical Light

        } else if (verticalLight.getColor() == TrafficLight.GREEN) {
            g.setColor(Color.GREEN);
            g.fillOval(getWidth() / 2 + 115, 15, 15, 15); // Draws green light for Vertical Light
        }

//        g.setColor(Color.ORANGE);
//        g.fillRect(5, getHeight() / 2 + 5, 10, 10); // Draws a motorbike
//
//        g.setColor(Color.PINK);
//        g.fillRect(18, getHeight() / 2 + 5, 15, 15); // Draws a car
//
//        g.setColor(Color.BLUE);
//        g.fillRect(55, getHeight() / 2 + 5, 40, 20); // Draws a truck



//        System.out.println("finished painting boss!"); //To show the display is getting repainted - Repaint is working properly

    }
}
